<?php


namespace Admin\Controller;

use Think\Controller;

class AuthController extends AdminController
{
    public function showList()
    {
        $auth_info = D('auth')->order('auth_path')->select();
        $this->assign('auth_info', $auth_info);
        $this->display();
    }

    public function add()
    {
        if (empty($_POST)) {
            $auth_info = D('auth')->order('auth_path')->select();
            $this->assign('auth_info', $auth_info);
            $this->display();
        } else {
            $auth_id = D('auth')->add($_POST);
            if ($_POST['auth_pid'] == 0) {
                $auth_path = $auth_id;
            } else {
                $p_auth = D('auth')->find($_POST['auth_pid']);
                $auth_path = $p_auth['auth_path'] . '-' . $auth_id;
            }
            $auth_level = substr_count($auth_path, '-');
            dump('level' + $auth_level);
            D('auth')->where("auth_id=$auth_id")->save([
                'auth_path' => $auth_path,
                'auth_level' => $auth_level
            ]);
//            $this->success('权限添加成功','showlist');
        }
    }
    public function del($auth_id){
        $res = D('auth')->delete($auth_id);
        if ($res !== false) {
            $this->redirect('showList', array(), 2, '删除权限成功');
        } else {
            $this->redirect('del', array(), 2, '删除权限失败');
        }
    }

}